package cn.itcast.web.controller.cargo;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping(value = "/cargo/contract", name = "合同管理")
public class ContractController extends BaseController {

    @Reference
    private ContractService contractService;

    @RequestMapping(value = "/list", name = "合同分页查询")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {

        // 1.创建合同查询对象
        ContractExample contractExample = new ContractExample();
        // 2.创建条件对象
        ContractExample.Criteria criteria = contractExample.createCriteria();
        // 3.指定 企业id 条件
        criteria.andCompanyIdEqualTo(getCompanyId());
        // 4.指定时间降序
        contractExample.setOrderByClause("create_time desc");

        // 细颗粒度权限管理 start
        User loginUser = getLoginUser();
        if (loginUser.getDegree() == 2) { // 管理下属部门
            criteria.andCreateDeptLike(loginUser.getDeptId() + "%");
        } else if (loginUser.getDegree() == 3) { // 管理本部门
            criteria.andCreateDeptEqualTo(loginUser.getDeptId());
        } else if (loginUser.getDegree() == 4) { // 普通员工
            criteria.andCreateByEqualTo(loginUser.getId());
        }
        // 细颗粒度权限管理 end

        // 5.分页查询
        PageInfo page = contractService.findByPage(pageNum, pageSize, contractExample);
        request.setAttribute("page", page);

        return "cargo/contract/contract-list";
    }

    @RequestMapping(value = "/toAdd", name = "新增页面")
    public String toAdd() {
        return "cargo/contract/contract-add";
    }

    @RequestMapping(value = "/edit", name = "保存或修改")
    public String edit(Contract contract) {

        if (StrUtil.hasEmpty(contract.getId())) {
            // 指定企业id、创建人id、部门id、创建时间
            contract.setCompanyId(getCompanyId());
            contract.setCreateBy(getLoginUser().getId());
            contract.setCreateDept(getLoginUser().getDeptId());
            contract.setCreateTime(new Date());

            contractService.save(contract);
        } else {
            // 指定修改人id、修改时间
            contract.setUpdateBy(getLoginUser().getUserName());
            contract.setUpdateTime(new Date());
            contractService.update(contract);

        }

        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "去修改页面")
    public String toUpdate(String id) {

        Contract contract = contractService.findById(id);
        request.setAttribute("contract", contract);
        return "cargo/contract/contract-update";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id) {
        contractService.delete(id);
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping(value = "/submit", name = "提交")
    public String submit(String id) {
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(1);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消")
    public String cancel(String id) {
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(0);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }
}
